package com.yonyou.crm.cum.customer.appctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.base.EnterpriseBaseCtrl;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;

@RestController
@RequestMapping(value = "/{version}/mcum/customers")
public class AppEnterpriseCtrl extends EnterpriseBaseCtrl {


	/**
	 * 获取客户地图分布列表
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "customermap", method = RequestMethod.GET)
	public Object getCustomerMap(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException{
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String deptid = paramObject.getString("deptid");
		JSONArray persons = paramObject.getJSONArray("persons");
		List<String> personIDs = new ArrayList<>();
		if(persons != null) {
			Object[] objects = persons.toArray();
			for(Object obj : objects) {
				personIDs.add(obj.toString());
			}
		}

		Map<String, Object> data = new HashMap<>();

		List<Map<String,Object>> cusmaplist =  service.getCustomerMap(deptid, personIDs);
		data.put("cusmaplist" , cusmaplist);
		return ResponseUtil.getObject(data);
	}

	/**
	 * 企业信息按名称查询
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "identifications", method = RequestMethod.GET)
	public Object getEnteInfoByName(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException{
		Object data = super.getEnteInfoByName(request);
		Map<String, Object> res = new HashMap<>();
		res.put("datalist", data);
		return ResponseUtil.getObject(res);
	}
	
	/**
	 * 企业信息精确查询
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "identifications/{id}", method = RequestMethod.GET)
	public Object getEnteInfoByCompanyId(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		Object list = super.getEnteInfoByCompanyId(id);
		Map<String, Object> res = new HashMap<>();
		res.put("detaillist", list);
		return ResponseUtil.getObject(res);
	}

	/**
	 * 审核、取消审核
	 * @param id
	 * @param request
	 * @param response
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}/identifications", method = RequestMethod.PUT)
	public Object identification(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		return super.identification(id, request);
	}
	
	/**
	 * 根据客户id获取审核信息
	 * @param id
	 * @param request
	 * @param response
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}/identifications", method = RequestMethod.GET)
	public Object getEnteInfoByCustomerId(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		Map<String, Object> res = super.getEnteInfoByCustomerId(id, request);
		res.put("detaillist", res.get("data"));
		res.remove("data");
		return ResponseUtil.getObject(res);
	}
	
	/**
	 * 根据公司Id获取是否认证标识
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "/identifications/identifyflag", method = RequestMethod.GET)
	public Object getIdentifyFlagByCompanyId(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String companyId = paramObject.getString("companyid");
		super.getIdentifyFlagByCompanyId(companyId);
		return ResponseUtil.getObject();
	}


	/**
	 * 获取客户相关对象
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "relatedlist", method = RequestMethod.GET)
	public Object getRelatedlist(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String customerId = paramObject.getString("objid");
		String type = paramObject.getString("type");
		List<Map<String,Object>> dataList = service.getCustomerRelatedlist(customerId, type);
		Map<String, Object> datamap = new HashMap<>();
		datamap.put("datalist", dataList);
		return ResponseUtil.getObject(datamap);
	}

	/**
	 * 移除相关对象关联关系
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "/del/{id}", method = RequestMethod.PUT)
	public Object delrelate(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String relateid = paramObject.getString("relateid");
		String type = paramObject.getString("type");
		service.delrelate(id, relateid, type);
		return ResponseUtil.getObject();
	}


	/**
	 * 增加业务对象参与人
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "/add/{id}", method = RequestMethod.PUT)
	public Object addrelate(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String subbnstype = paramObject.getString("subbnstype");
		JSONArray partlist = paramObject.getJSONArray("partlist");
		if(partlist != null && partlist.size() > 0 ) {
			List<String> relateArry = new ArrayList<>();
			Object[] partArry = partlist.toArray();
			for(Object obj : partArry) {
				relateArry.add(obj.toString());
			}
			service.addrelate(id, relateArry);
		} else {
			throw  new CrmMessageException("请选中要分配的参与人");
		}
		return ResponseUtil.getObject();
	}
}


